package cn.deschen.eshop.user.config;

import cn.deschen.eshop.auth.config.DefaultSecurityConfig;
import cn.deschen.eshop.auth.service.DynamicSecurityService;
import cn.deschen.eshop.generator.model.UmsResource;
import cn.deschen.eshop.user.service.UmsAdminService;
import cn.deschen.eshop.user.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author hanbin_chen
 * @Description 电子商城安全配置
 * @DateTime 2021/1/14 10:01
 * @Version V1.0.0
 */
@Configuration
public class EshopSecurityConfig extends DefaultSecurityConfig {

    @Autowired
    private UmsAdminService adminService;

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 注册security认证用的用户信息service
     *
     * @return
     */
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        // lamdba表达式，但前接口只有一个方法时
        //获取登录用户信息
        return username -> adminService.loadUserByUsername(username);
    }

    /**
     * todo 报循环依赖异常，目前使用组件解决
     * 注册DynamicSecurityFilter动态资源service，功能：映射资源接口与资源认证信息
     * @return
     */
    /*
    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        // lamdba表达式，但前接口只有一个方法时
        return () -> {
            List<UmsResource> resourceList = umsResourceService.getResourceList();
            return resourceList.stream().collect(
                    Collectors.toMap(
                            UmsResource::getUrl,
                            resource -> new SecurityConfig(resource.getId() + ":" + resource.getName())
                    )
            );
        };
    }*/
}
